﻿@inject IDataCacheService _dataCacheService


<MDCard>
    <MTreeview @bind-Open="@initiallyOpen"
               Active="new List<long> { Id}"
               Items="Items"
               TItem="DocumentViewModel"
               TKey="long"
               ItemKey="u => u.Id"
               ItemChildren="u => u.Children"
               ItemText="u => u.Title"

               Activatable
               OpenOnClick
               
               ActiveChanged="ActiveChanged">
        <PrependContent>
            @if (string.IsNullOrWhiteSpace(context.Item.Icon) == false)
            {
                <MIcon>
                    @context.Item.Icon
                </MIcon>
            }
        </PrependContent>
    </MTreeview>
</MDCard>

@code {
    [Parameter]
    public List<DocumentViewModel> Items { get; set; } = new List<DocumentViewModel>();
    [Parameter]
    public long Id { get; set; }
    [Parameter]
    public EventCallback<long> IdChanged { get; set; }

    List<long> initiallyOpen = new();

    protected override async Task OnInitializedAsync()
    {
        ChangeOpen(Id);
    }

    public void ChangeOpen(long id)
    {
        var item = Items.FirstOrDefault(s => s.Children.Any(s => s.Id == id));
        if (item != null)
        {
            initiallyOpen.Clear();
            initiallyOpen.Add(item.Id);
        }
    }

    public async Task ActiveChanged(List<long> items)
    {
        if(items.Any()==false)
        {
            return;
        }
        Id = items.First();
        ChangeOpen(Id);
        StateHasChanged();
        await IdChanged.InvokeAsync(Id);
    }

}
